package com.yld.oa.employee.service.impl;

import java.sql.Connection;
import java.sql.SQLException;

import com.yld.oa.department.dao.DepartmentDao;
import com.yld.oa.department.dao.impl.DepartmentDaoImpl;
import com.yld.oa.department.entity.DepartmentEntity;
import com.yld.oa.employee.dao.EmployeeDao;
import com.yld.oa.employee.dao.impl.EmployeeDaoImpl;
import com.yld.oa.employee.entity.EmployeeEntity;
import com.yld.oa.employee.service.EmployeeService;
import com.yld.util.DBUtil;


/**
 * 在 service层处理事务...
 * @author apples
 *
 */
public class EmployeeServiceImpl implements EmployeeService {

	private EmployeeDao employeeDao= new EmployeeDaoImpl();
	
	private DepartmentDao  DepartmentDao= new DepartmentDaoImpl();
	
	
	private Connection connection = DBUtil.getConnection();
	
	@Override
	public void addEmployeeAndDepartment(EmployeeEntity employee,
			DepartmentEntity department) {

		try {
			
			//1 在操作表之前  把自己提交该成手动提交
			connection.setAutoCommit(false);
			
			//保存员工
			employeeDao.addEmployee(employee,connection);
			
			//System.out.println(1/0);
			
			//保存部门
			DepartmentDao.addDepartment(department,connection);
			
			//2手动提交事务
			connection.commit();
		} catch (Exception e) {
			
			//3 手动回滚事务
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		
		//保证事务?
	}

}
